package com.css.fxfzxczh.modules.report.controller;

import com.css.fxfzxczh.base.exception.CustomException;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.report.entity.Report;
import com.css.fxfzxczh.modules.report.service.ReportService;
import com.css.fxfzxczh.modules.report.vo.CreateReportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;

    //报告编号生成
    @GetMapping("/getNumber")
    public RestResponse getNumber(@RequestParam String id){
        RestResponse restResponse = null;
        try {
            String number = reportService.getNumber(id);
            restResponse = RestResponse.succeed(number);
        } catch (Exception e) {
            String errormessage = "获取编号生成失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    //生成报告
    @PostMapping("/createReport")
    public RestResponse createReport(HttpServletResponse response ,@RequestBody CreateReportVo vo){
        RestResponse restResponse = null;
        try {
            reportService.createReport(response ,vo);
            restResponse = RestResponse.succeed("保存成功");
        } catch (CustomException e) {
            log.error(e.getMessage());
            restResponse = RestResponse.fail(e.getMessage());
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //获取所有报告
    @GetMapping("/getReports")
    public RestResponse getReports(@RequestParam String xsemId){
        RestResponse restResponse = null;
        try {
            List<Report> reports = reportService.getReports(xsemId);
            restResponse = RestResponse.succeed(reports);
        } catch (Exception e) {
            String errormessage = "获取所有报告失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //上传覆盖报告
    @PostMapping("/updateReport")
    public RestResponse updateReport(@RequestBody Report report){
        RestResponse restResponse = null;
        try {
            reportService.updateReport(report);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            String errormessage = "上传失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
